/****************************************************************
 * <strong>MFDataColumns虚拟化数据表</strong>
 *
 * <p>
 *MFDataColumns虚拟化数据表 
 * </p>
 *
 * @author AndyZhang 2011 cnet2001@163.com
 * @version 0.1
 * ToDo:2011-11-29 支持 BigTable
 ***************************************************************/
package com.thinkwide.data.ormapping.datatable;

import com.thinkwide.data.core.MFString;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class MFDataColumns implements Iterable<MFDataColumn> {

    private ArrayList<MFDataColumn> maps;

    public MFDataColumns() {
        maps = new ArrayList<MFDataColumn>();

    }

    public MFDataColumns clone(MFDataColumns columns) {

        for (MFDataColumn col : columns) {
            MFDataColumn column = MFDataColumn.clone(col);
            this.addColumn(column);
        }

        return this;
    }

    public void setViewType() {
        for (MFDataColumn col : this) {
            String dataType = col.getDataType();
            if (MFString.compareNoCase(dataType, "date")) col.setViewType(MFDataColumnViewType.datePicker);
        }
    }

    public MFDataColumn addColumn(MFDataColumn column) {
        maps.add(column);
        int i = maps.indexOf(column);
        column.setIndex(i);
        return column;
    }

    public void removeColumn(int index) {
        maps.remove(index);
    }

    public void removeColumn(MFDataColumn column) {
        maps.remove(column);

    }

    public void clear() {
        maps.clear();

    }

    public void add(int index, MFDataColumn column) {
        maps.add(index, column);

    }

    public int indexOf(MFDataColumn column) {
        return maps.indexOf(column);

    }

    public int indexOf(String fieldName) {
        MFDataColumn column = getColumn(fieldName);
        if (column == null)
            return -1;
        else
            return maps.indexOf(column);

    }

    public int count() {
        return maps.size();

    }

    public MFDataColumn getColumn(int index) {

        return maps.get(index);

    }

    public MFDataColumn getColumn(String fieldName) {
        for (MFDataColumn item : maps) {
            if (MFString.compareNoCase(item.getField(), fieldName))
                return item;
        }
        return null;

    }

    public boolean hasColumn(String fieldName) {
        return (getColumn(fieldName) != null);
    }

    public List<String> getColumnNames() {
        List<String> list = new ArrayList<String>();
        for (MFDataColumn item : maps) {
            list.add(item.getField());
        }
        return list;

    }

    public Iterator<MFDataColumn> iterator() {

        Iterator<MFDataColumn> it =
                new Iterator<MFDataColumn>() {
                    private int index = 0;

                    public boolean hasNext() {

                        return index < maps.size();
                    }

                    public MFDataColumn next() {
                        return maps.get(index++);
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };

        return it;
    }

}
